<?php

namespace app\teacher\controller\v1;

use app\common\controller\BaseController;
use think\Controller;
use think\Db;
use think\Request;
use app\teacher\model\TeacherCourse as TeacherCourseModel;
use app\teacher\model\CourseComment as CourseCommentModel;
use app\teacher\model\CourseReply as CourseReplyModel;
use app\common\model\RemindOrder as RemindOrderModel;
use app\teacher\model\CourseOrder as CourseOrderModel;

class Comment extends BaseController
{
    /**
     * 评论
     */
    public function index()
    {
        $size = $this->request->get('size');
        $teacherCourseId = TeacherCourseModel::getTeacherCourseId($this->teacherId);
        $comments = CourseCommentModel::getComments($teacherCourseId, $size);
        $comments ? $this->success('', $comments) : $this->notFoundData();
    }

    /**
     * 教师回复
     */
    public function replay(CourseReplyModel $courseReplyModel)
    {
        $this->isPost();
        $courseCommentId = $this->notEmpty('course_comment_id');
        $content = $this->notEmpty('content');
        $courseComment = CourseCommentModel::find($courseCommentId);
        if (!$courseComment) {
            $this->errorMsg(13011);
        }
        $courseOrder = CourseOrderModel::find($courseComment->course_order_id);
        if (!$courseOrder) {
            $this->errorMsg(13011);
        }
        Db::startTrans();
        try {
            $courseReplyModel->add($this->teacherId, $courseCommentId, $content);
            //订单提醒
            RemindOrderModel::add($courseOrder, 5);

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        isset($e) ? $this->forbidden() : $this->success();
    }
}
